#!/bin/bash
# script name: mysql编译安装脚本
# author: subushi
# update date: 2023.08.15

# 进入编译路劲
cd /usr/local/src/


# 下载源码包
wget --user= --ask http://mydata.subushi.top/Linux%E5%8C%85%E5%BA%93/mysql-5.7%E6%BA%90%E7%A0%81%E5%8C%85/mysql-5.7.37.tar.gz
wget --user= --ask http://mydata.subushi.top/Linux%E5%8C%85%E5%BA%93/mysql-5.7%E6%BA%90%E7%A0%81%E5%8C%85/boost_1_59_0.tar.gz


# 解压
tar xf boost_1_59_0.tar.gz
mv boost_1_59_0  /usr/local/boost
tar xf mysql-5.7.37.tar.gz
cd mysql-5.7.37/


# 安装编译相关工具
yum  -y  install  gcc-c++  ncurses-devel  cmake  openssl  openssl-devel


# 新建用户/组
groupadd  -g  27  mysql
useradd  -u  27  -g  mysql  -M  -s  /sbin/nologin  mysql


# 编译安装
mkdir  /data/mysql && chown mysql:mysql  /data/mysql/ && ls -ld /data/mysql

cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 -DMYSQL_DATADIR=/data/mysql  -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1    -DDOWNLOAD_BOOST=1  -DWITH_BOOST=/usr/local/boost    -DWITH_INNODB_MEMCACHED=ON

make
make install


# 提供配置文件
echo "[client]
port=3306
socket=/data/mysql/mysql.sock
user=root
#password=123123

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=200
max_connect_errors=50
#客户端一次最大允许提交多大体积
max_allowed_packet=100M

skip-ssl
#have_openssl=NO
#have_ssl=NO
#ssl_ca=ca.pem
#ssl_cert=server-cert.pem
#ssl_key=server-key.pem

#日志时间戳
log_timestamps=system
#日志相关
general_log=OFF
log_output=file
general_log_file=/data/mysql/general.log
slow_query_log=ON
slow_query_log_file=/data/mysql/slow.log
long_query_time=3.000000
#log-bin=/log-bin/mysql-bin
#server-id=51
binlog_format=row
binlog_rows_query_log_events=ON
max_binlog_size=1073741824
expire_logs_days=0

#事务提交机制
innodb_file_per_table=ON
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16777216
innodb_log_file_size=50331648
innodb_log_files_in_group=2
innodb_log_group_home_dir=/data/mysql
#隔离级别
transaction_isolation=READ-UNCOMMITTED
#缓存
query_cache_size=128M
query_cache_type=ON
query_cache_wlock_invalidate=OFF

#skip-grant-tables 
skip-name-resolve
user=mysql
port=3306
basedir=/usr/local/mysql57
datadir=/data/mysql
tmpdir=/tmp
socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /etc/my.cnf


# 删除源码目录
rm -rf /usr/local/src/mysql-5.7.37


# 编写环境变量
echo "PATH=$PATH:/usr/local/mysql57/bin
export  PATH" >> /etc/profile
source /etc/profile


# 初始化数据库
cd /data/mysql
mysqld  --defaults-file=/etc/my.cnf  --initialize


# 启动MySQL
/usr/local/mysql57/support-files/mysql.server start


# 查看生成的初始密码
cat mysqld.log | grep password


echo "输入mysql，如果tap不出快捷，再手动执行一次 source /etc/profile，也可以尝试将脚本中的环境变量写进 /etc/bashrc"
echo "登录后会提示需立即修改密码，使用 ALTER USER 'root'@'localhost' IDENTIFIED BY '密码'; 刷新权限表 flush privileges;"
